package com.kun.service_train.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.service_train.entity.Student;
import com.kun.service_train.entity.Train;
import com.kun.service_train.service.TrainService;
import com.kun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-05
 */
@RestController
@RequestMapping("/service_train/train")
@CrossOrigin
public class TrainController {
    @Autowired
    private TrainService trainService;
    @PostMapping("addTrain")
    public R addTrain(@RequestBody Train train){
        System.out.println(train);
        boolean save = trainService.save(train);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @PostMapping("delTrainById/{id}")
    public R delTrainById(@PathVariable String id){
        boolean b = trainService.removeById(id);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @PostMapping("getTrain/{current}/{limit}")
    public R getTrain(@PathVariable Long current,@PathVariable Long limit,@RequestBody(required = false) Train train){
        Page<Train> page = new Page<>(current,limit);
        QueryWrapper<Train> wrapper = new QueryWrapper<>();
        String tName = train.getTeacher();
        String address = train.getAddress();


        if (!ObjectUtils.isEmpty(tName)){
            wrapper.like("teacher",tName);
        }
        if (!ObjectUtils.isEmpty(address)){
            wrapper.like("address",address);
        }

        IPage<Train> page1 = trainService.page(page, wrapper);
        return R.ok().data("list",page1);
    }
    @PostMapping("getTrainById/{id}")
    public  R getTrainById(@PathVariable String id){
        Train train = trainService.getById(id);
        return R.ok().data("train",train);
    }
    @PostMapping("updateTrain")
    public R updateTrain(@RequestBody Train train) {
        boolean b = trainService.updateById(train);
        return R.ok();
    }
//    @Scheduled(cron = "* * * * * ?")
//    public void delByTIme() throws ParseException {
//
//    }
//    @Scheduled(cron = "* * * * * ?")
    public R asd(){
        QueryWrapper<Train> wrapper = new QueryWrapper<>();
        wrapper.eq("state",1);
        List<Train> list = trainService.list(wrapper);
        return R.ok().data("list",list);
    }
}

